package com.example.lay.io;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;

/**
 * Created by Lay on 2014/12/8.
 */
public class MySDFileActivity extends Activity {
    private final static String FILE_NAME = "/wscl.bin";
    private static String sdCardDir = "";

    {
        try {
            sdCardDir = Environment.getExternalStorageDirectory().getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_file_sd);
        ((Button) findViewById(R.id.btnWriteFileSD)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//如果加载SD卡
                    File targetFile = new File(sdCardDir + FILE_NAME);
                    RandomAccessFile out = null;
                    try {
                        out = new RandomAccessFile(targetFile, "rw");
                        out.seek(targetFile.length());
                        out.write(((EditText) findViewById(R.id.eTxtWriteSD)).getText().toString().getBytes());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        });
        ((Button) findViewById(R.id.btnReadFileSD)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BufferedReader br = null;
                StringBuffer sb = null;
                try {
                    FileInputStream file = new FileInputStream(sdCardDir + FILE_NAME);
                    br = new BufferedReader(new InputStreamReader(file));
                    String line = null;
                    sb = new StringBuffer();
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (br != null) br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                ((EditText) findViewById(R.id.eTxtReadSD)).setText(sb);
            }
        });
    }
}
